﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTFF.WPF.Helpers;
using iTFF.WPF.Model.Wikipedia;
using System.IO;
using System.Text.RegularExpressions;

namespace iTFF.WPF.CP
{
    class WikipediaControlPanel
    {

        private WikipediaParser _wikipediaParser;

        private WikipediaParser WikipediaParser
        {
            get
            {
                if (_wikipediaParser == null)
                    _wikipediaParser = new WikipediaParser();
                return _wikipediaParser;
            }
        }
        

        public WikipediaParseResult DownloadWikipediaArticleXaml(string url)
        {
            string result = WebHelper.SubmitRequestAndGetResponse(url);

            if (result != null)
            {
                string export = WikipediaParser.GetAPIResultExportPart(result);

                var results = WikipediaParser.ParseXMLExportToFlowDocumentXaml(export, "http://en.wikipedia.org/wiki/");
                DownloadAllImagesPathesForResult(results);
                WikipediaParser.ReplaceImagePathes(results);
                return results;
            }
            else
                return null;
        }

        private void DownloadAllImagesPathesForResult(WikipediaParseResult result)
        {
            foreach (var image in result.Images)
            {
                // get the url
                image.FileURL = GetImageURL(image.Name, image.Width, image.Height);
                //image.LocalFileStream = DownloadImage(image.FileURL);
            }
        }

        private MemoryStream DownloadImage(Uri url)
        {

            // download the data
            var imageBytes = new System.Net.WebClient().DownloadData(url);

            // copy to memorystream
            MemoryStream stream = new MemoryStream();
            stream.Write(imageBytes, 0, imageBytes.Length);
            stream.Seek(0, SeekOrigin.Begin);

            return stream;

        }

        //http://en.wikipedia.org/w/api.php?action=query&format=xml&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo&iiprop=url&iiurlwidth=200
        private Uri GetImageURL(string imageName, int? width, int? height)
        {
            string url = "http://{0}.wikipedia.org/w/api.php?action=query&format=xml&titles=Image:{1}&prop=imageinfo&iiprop=url{2}{3}";

            string sWidth= string.Empty;
            string sHeight= string.Empty;

            if (width.HasValue)
                sWidth = "&iiurlwidth=" + width.Value.ToString();
            if (height.HasValue)
                sHeight = "&iiurlheight=" + height.Value.ToString();

            string realUrl = string.Format(url, "en", imageName, sWidth, sHeight);

            string result = WebHelper.SubmitRequestAndGetResponse(realUrl);

            if (result != null)
            {
                return new WikipediaParser().ParseImageUrl(result, width.HasValue);
            }
            else
                return null;

        }

    }
}
